﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace Jy123.Web.Manage
{
    public partial class ULevel_QueueName : MgConfig
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("ULevel_QueueName.aspx");
            if (!Page.IsPostBack)
            {
                LoadInitialValue();
            }
        }

        protected void Repeater_DataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Jy123.Model.ULevel_QueueName queueName = e.Item.DataItem as Jy123.Model.ULevel_QueueName;
                HiddenField idHidden = e.Item.FindControl("idHidden") as HiddenField;
                idHidden.Value = queueName.ID.ToString();
                Label cNameLabel = e.Item.FindControl("cNameLabel") as Label;
                cNameLabel.Text = queueName.CName;
                Label nameLabel = e.Item.FindControl("queueNameLabel") as Label;
                nameLabel.Text = queueName.QueueName;
            }
        }

        protected void Repeater_Command(object sender, RepeaterCommandEventArgs e)
        {
            RepeaterItem item = e.Item as RepeaterItem;
            HiddenField hidden = e.Item.FindControl("idHidden") as HiddenField;
            int id = int.Parse(hidden.Value);
            Jy123.BLL.ULevel_QueueName BLLULQueueName = new Jy123.BLL.ULevel_QueueName();
            if (e.CommandName.Equals("Edit"))
            {
                Jy123.Model.ULevel_QueueName queueName = BLLULQueueName.GetModel(id);
                actionTypeHidden.Value = "Update";
                idHidden.Value = id.ToString();
                cNameTextBox.Text = queueName.CName;
                queueNameTextBox.Text = queueName.QueueName;
            }
            else if (e.CommandName.Equals("Delete"))
            {
                BLLULQueueName.Delete(id);
                LoadInitialValue();
                ClearPage();
            }
        }

        protected void Save(object sender, EventArgs e)
        {
            Jy123.BLL.ULevel_QueueName BLLULQueueName = new Jy123.BLL.ULevel_QueueName();
            if (actionTypeHidden.Value.Equals("Add"))
            {
                Jy123.Model.ULevel_QueueName queueName = new Jy123.Model.ULevel_QueueName();
                SetValue(queueName);
                BLLULQueueName.Add(queueName);
            }
            else
            {
                int id = int.Parse(idHidden.Value);
                Jy123.Model.ULevel_QueueName queueName = BLLULQueueName.GetModel(id);
                SetValue(queueName);
                BLLULQueueName.Update(queueName);
            }
            LoadInitialValue();
            ClearPage();
        }

        private void SetValue(Jy123.Model.ULevel_QueueName queueName)
        {
            queueName.CName = cNameTextBox.Text;
            queueName.QueueName = queueNameTextBox.Text;
        }

        private void ClearPage()
        {
            actionTypeHidden.Value = "Add";
            cNameTextBox.Text = string.Empty;
            queueNameTextBox.Text = string.Empty;
        }

        private void LoadInitialValue()
        {
            Jy123.BLL.ULevel_QueueName BLLULQueueName = new Jy123.BLL.ULevel_QueueName();
            List<Jy123.Model.ULevel_QueueName> listQueueName = BLLULQueueName.GetModelList(string.Empty);
            queueNameRepeater.DataSource = listQueueName;
            queueNameRepeater.DataBind();
            queueNameRepeater.Visible = listQueueName.Count > 0;
        }
    }
}
